package com.kennedy.springbootjpa.web.controller;

import com.kennedy.springbootjpa.beans.Shop;
import com.kennedy.springbootjpa.beans.common.ResultMsg;
import com.kennedy.springbootjpa.beans.menu.Menu;
import com.kennedy.springbootjpa.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by tao.zeng on 2017/8/14.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/toAdd")
    public String toAdd() {
        return "/menu/add";
    }

    @GetMapping("/toList")
    public String toList() {
        return "/menu/list";
    }

    @GetMapping("/getListByPage")
    @ResponseBody
    public Page<Menu> getListByPage(Menu menu) {
        menu.setText(menu.getSearchText());
        Page<Menu> menus = menuService.getListByPage(menu);
        return menus;
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultMsg add(Menu menu) {
        menuService.add(menu);
        return ResultMsg.success();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultMsg get(@PathVariable("id") long id) {
        menuService.delete(id);
        return ResultMsg.success();
    }
}
